from binance.client import Client
from collections import defaultdict
import json
import time
from datetime import datetime
from binance.websocket.um_futures import websocket_client


def on_open(ws):
    print("hello")
a= websocket_client.UMFuturesWebsocketClient(
    on_open=on_open
)


client = Client()

depth = client.futures_order_book(symbol="BTCUSDT", limit=1000)  # 获取最多1000条数据


# 初始化分类结果
buy_levels = defaultdict(float)   # 买单分类汇总
sell_levels = defaultdict(float)  # 卖单分类汇总

# 定义一个函数来根据价格分类
def categorize_price(price):
    return int(float(price) // 0.1 * 0.1)


# 处理买单数据
for price, quantity in depth['bids']:
    level = categorize_price(price)
    buy_levels[level] += float(quantity)

# 处理卖单数据
for price, quantity in depth['asks']:
    level = categorize_price(price)
    sell_levels[level] += float(quantity)

print("\n卖单分类 (每100美元):")
for level in sorted(sell_levels.keys()):
    print(f"价格区间: {level} - {level+99}, 数量: {sell_levels[level]:.4f} BTC")

# 按区间输出分类后的结果
print("\n买单分类 (每100美元):")
for level in sorted(buy_levels.keys(), reverse=True):
    print(f"价格区间: {level} - {level+99}, 数量: {buy_levels[level]:.4f} BTC")